www.gusucode.com > BCB 写的一款可自定义风格的俄罗斯方块源码 > BCB 写的一款可自定义风格的俄罗斯方块源码/code/俄罗斯方块/Unit1.cpp

    //Download by http://www.NewXing.com
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"
#include <stdlib.h>
#include <math.h>
#include <mmsystem.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TTetrisForm *TetrisForm;
//---------------------------------------------------------------------------
__fastcall TTetrisForm::TTetrisForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------



void __fastcall TTetrisForm::FormCreate(TObject *Sender)
{

     Bevel3->Top= Tp-FieldWidth;
     Bevel3->Left= Lft-FieldWidth;
     Bevel3->Width= GlassWidth*NormalWidth+FieldWidth*2;
     Bevel3->Height= GlassHeight*NormalHeight+FieldWidth*2;

     Image->Top=Tp;
     Image->Left=Lft;
     Image->Width=GlassWidth*NormalWidth;
     Image->Height=GlassHeight*NormalHeight;
     BGImage->Width=Image->Width;
     BGImage->Height=Image->Height;
     
     ClientWidth= Bevel3->Width+FieldWidth*3+Button1->Width;
     ClientHeight= Bevel3->Height+FieldWidth*2;
     Button1->Left = Bevel3->Width+FieldWidth*2;
     Button2->Left = Button1->Left;
     Button3->Left = Button1->Left;
     Button5->Left = Button1->Left;
     Button4->Left = Button1->Left;

     Label1->Left= Bevel3->Width+FieldWidth*2;
     Label2->Left= Label1->Left;
     Bevel1->Left= Label1->Left;
     Bevel1->Width= Button1->Width;
     Bevel2->Left= Label1->Left;
     Bevel2->Width= Button1->Width;
     Label3->Left = Bevel1->Left+FieldWidth;
     Label4->Left = Bevel1->Left+FieldWidth;
     Bevel4->Top = Button5->Top+Button5->Height+4;
     Bevel4->Left = Button5->Left;
     Bevel4->Height = SmallHeight*4+4;
     Bevel4->Width =SmallWidth*4+8;
     NextImage->Top = Button5->Top+Button5->Height+6;
     NextImage->Left = Button5->Left+4;
     NextImage->Height = SmallHeight*4;
     NextImage->Width =SmallWidth*4;

     randomize();
     ReadStyle();
     for (int i=0;i<4;i++)
          for (int j=0;j<4;j++)
               ClearSmallBlock(i,j);
     ReadIni();
     LoadImage(ImageFileName);
     isPlaying=false;
     Timer1->Enabled=false;
     MediaPlayer1->FileName=ExtractFilePath(Application->ExeName)
     +"\\"+"BGSound.mid";
     MediaPlayer1->Open();

     ConnectJoy();
}
//---------------------------------------------------------------------------


void __fastcall TTetrisForm::DrawBlock(int X, int Y, TColor mColor)
{
     int X1,Y1,X2,Y2;
     X1 = X*NormalWidth;
     X2 = X1+NormalWidth;
     Y1 = Y*NormalHeight;
     Y2 = Y1+NormalHeight;
     Image->Canvas->Brush->Color=mColor;
     Image->Canvas->FillRect(Rect(X1+1,Y1+1,X2-1,Y2-1));
     Image->Canvas->Pen->Color = clGray;
     Image->Canvas->MoveTo(X1,Y1);
     Image->Canvas->LineTo(X1,Y2-1);
     Image->Canvas->LineTo(X2-1,Y2-1);
     Image->Canvas->Pen->Color = clWhite;
     Image->Canvas->LineTo(X2-1,Y1);
     Image->Canvas->LineTo(X1,Y1);
}



void __fastcall TTetrisForm::ClearBlock(int X, int Y)
{
     int X1,Y1,X2,Y2;
     X1 = X*NormalWidth;
     X2 = X1+NormalWidth;
     Y1 = Y*NormalHeight;
     Y2 = Y1+NormalHeight;
     Image->Canvas->CopyRect(Rect(X1,Y1,X2,Y2),
          BGImage->Canvas,Rect(X1,Y1,X2,Y2));
}




void __fastcall TTetrisForm::Button1Click(TObject *Sender)
{
     Start();
     Button1->Enabled=false;
     Button2->Enabled=true;
     Button3->Enabled=true;
     N2->Enabled=false;
}
//---------------------------------------------------------------------------


void __fastcall TTetrisForm::DrawSmallBlock(int X, int Y, TColor mColor)
{
     int X1,X2,Y1,Y2;
     X1 = X*SmallWidth;
     X2 = X1+SmallWidth;
     Y1 = Y*SmallHeight;
     Y2 = Y1+SmallHeight;
     NextImage->Canvas->Brush->Color = mColor;
     NextImage->Canvas->FillRect(Rect(X1+1,Y1+1,X2-1,Y2-1));
     NextImage->Canvas->Pen->Color = clGray;
     NextImage->Canvas->MoveTo(X1,Y1);
     NextImage->Canvas->LineTo(X1,Y2-1);
     NextImage->Canvas->LineTo(X2-1,Y2-1);
     NextImage->Canvas->Pen->Color = clWhite;
     NextImage->Canvas->LineTo(X2-1,Y1);
     NextImage->Canvas->LineTo(X1,Y1);
}


void __fastcall TTetrisForm::ClearSmallBlock(int X, int Y)
{
     int X1,X2,Y1,Y2;
     X1 = X*SmallWidth;
     X2 = X1+SmallWidth;
     Y1 = Y*SmallHeight;
     Y2 = Y1+SmallHeight;
     NextImage->Canvas->Brush->Color = clBlack;
     NextImage->Canvas->FillRect(Rect(X1,Y1,X2,Y2));
}


void TTetrisForm::ReadStyle()
{
     for (int i=0; i<21; i++)
     for (int j=0; j<40; j++)
          if (Tetris[i][j]=='-')
               Style[i][j]=0;
          else Style[i][j]=Tetris[i][j]-'0';

}


void TTetrisForm::WriteNext()
{
     NextPatn=floor(random(21));
     NextRotat = floor(random(4));
     NextColor = floor(random(7));
     for (int i=0;i<4;i++)
     for (int j=0;j<4;j++)
          ClearSmallBlock(i,j);
     for (int i=0;i<4;i++)
          DrawSmallBlock(Style[NextPatn][NextRotat*10+i*2]-1,
                         Style[NextPatn][NextRotat*10+i*2+1]-1,
                         Colors[NextColor]);
}


void __fastcall TTetrisForm::WriteObject()
{
     if(MoveFlag)
          for (int i=0;i<4;i++)
               ClearBlock(OldX[i],OldY[i]);
     MoveFlag=true;
     for (int i=0;i<4;i++){
          OldX[i]=ObjectX+Style[Patn][Rotat*10+i*2]-1;
          OldY[i]=ObjectY+Style[Patn][Rotat*10+i*2+1]-1;
          DrawBlock(OldX[i],OldY[i],Colors[ObjectColor]);
     }
}


void __fastcall TTetrisForm::Start()
{
     if(PlayMusic)
          MediaPlayer1->Play();
     for (int i=0;i<GlassWidth;i++)
          for (int j=0;j<GlassHeight;j++){
               GlassMap[i][j]=0;
               ClearBlock(i,j);
     }
     Level = 1;
     Score = 0;
     Label3->Caption="1";
     Label4->Caption="0";
     MoveFlag=false;
     ObjectX=3; ObjectY=0;
     WriteNext();
     Patn = NextPatn;
     ObjectColor = NextColor;
     Rotat=NextRotat;
     WriteObject();
     WriteNext();
     isPlaying=true;
     Timer1->Interval=800-Level*100;
     Timer1->Enabled=true;
}



void __fastcall TTetrisForm::MoveRight()
{
     int xx=Style[Patn][Rotat*10]+ObjectX;
     if (xx <10){
     xx=Style[Patn][Rotat*10+2]+ObjectX;
     if (xx <10){
     xx=Style[Patn][Rotat*10+4]+ObjectX;
     if (xx <10){
     xx=Style[Patn][Rotat*10+6]+ObjectX;
     if (xx <10){
          if (HitCheck(ObjectX+1,ObjectY,Rotat)){
               ObjectX++;
               WriteObject();
          }
     }
     }
     }
     }
}

void __fastcall TTetrisForm::MoveLeft()
{
     int xx=Style[Patn][Rotat*10]+ObjectX-2;
     if (xx >=0){
     xx=Style[Patn][Rotat*10+2]+ObjectX-2;
     if (xx >=0){
     xx=Style[Patn][Rotat*10+4]+ObjectX-2;
     if (xx >=0){
     xx=Style[Patn][Rotat*10+6]+ObjectX-2;
     if (xx >=0){
          if (HitCheck(ObjectX-1,ObjectY,Rotat)){
               ObjectX--;
               WriteObject();
          }
     }
     }
     }
     }

}

bool __fastcall TTetrisForm::HitCheck(int X, int Y,int Ro)
{
     int xx,yy;
     for (int i=0;i<4;i++){
          xx=X+Style[Patn][Ro*10+i*2]-1;
          yy=Y+Style[Patn][Ro*10+i*2+1]-1;
          if(GlassMap[xx][yy]!=0) return false;
     }
     return true;
}

void __fastcall TTetrisForm::FormKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
     if (!isPlaying) return;
     switch(Key){
     case VK_LEFT:
          MoveLeft();
          break;
     case VK_RIGHT:
          MoveRight();
          break;
     case VK_UP:
          Rotate();
          break;
     case VK_DOWN:
          MoveDown();
          break;
     }
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::Rotate()
{
     int Ro=(Rotat+1)%4;
     int xx,yy;
     for (int i=0;i<4;i++){
          xx=ObjectX+Style[Patn][Ro*10+i*2]-1;
          yy=ObjectY+Style[Patn][Ro*10+i*2+1]-1;
          if (xx>9||xx<0||yy>22||yy<0) return;
     }
     if (HitCheck(ObjectX,ObjectY,Ro)){
          Rotat=Ro;
          WriteObject();
     }
}


void __fastcall TTetrisForm::MoveDown()
{
     bool chk=false;
     int xx,yy=Style[Patn][Rotat*10+1]+ObjectY;
     if (yy<23){
     yy=Style[Patn][Rotat*10+3]+ObjectY;
     if (yy<23){
     yy=Style[Patn][Rotat*10+5]+ObjectY;
     if (yy<23){
     yy=Style[Patn][Rotat*10+7]+ObjectY;
     if (yy<23){
          if (chk=HitCheck(ObjectX,ObjectY+1,Rotat)){
               ObjectY++;
               WriteObject();
          }
     }
     }
     }
     }
     if (!chk){
          Score+=25;
          if(PlaySnd) sndPlaySound("Tick.wav",SND_ASYNC);
          // 存储方块数据
          for (int i=0;i<4;i++){
               xx=ObjectX+Style[Patn][Rotat*10+i*2]-1;
               yy=ObjectY+Style[Patn][Rotat*10+i*2+1]-1;
               GlassMap[xx][yy]=ObjectColor+1;
          }
          ScanLines();
          if (Score>4000)  Level = 2;
          if (Score>8000)  Level = 3;
          if (Score>13000) Level = 4;
          if (Score>20000) Level = 5;
          if (Score>30000) Level = 6;
          if (Score>50000) Level = 7;
          Timer1->Interval = 800-Level*100;
          if (Level!=StrToInt(Label3->Caption)&&PlaySnd)
               sndPlaySound("Upgrade.wav",SND_ASYNC);
          Label3->Caption=AnsiString(Level);
          Label4->Caption=AnsiString(Score);
          //new block put screen
          ObjectX=3;
          ObjectY=0;
          Patn=NextPatn;
          Rotat=NextRotat;
          ObjectColor=NextColor;
          MoveFlag=false;
          WriteObject();
          if(!HitCheck(ObjectX,ObjectY,Rotat)){
               Timer1->Enabled=false;
               isPlaying=false;
               MessageBox(Handle,"抱歉!您失败了……","游戏结束",
                    MB_ICONINFORMATION|MB_OK);
               MediaPlayer1->Stop();
               AddHighScore();
               Button1->Enabled=true;
               N2->Enabled=true;
               Button2->Enabled=false;
               Button3->Enabled=false;
          }
          WriteNext();
     }
}


void __fastcall TTetrisForm::ScanLines()
{
     int Lines=0;

     for (int i=0;i<GlassHeight;i++)
     for (int j=0;j<GlassWidth;j++)
          OldGlassMap[j][i]=GlassMap[j][i];

     for (int i= 0;i<GlassHeight;i++){
          int k= 0;
          for (int j=0;j<GlassWidth;j++)
               if (GlassMap[j][i]>0) k++;
               if (k==GlassWidth){
                    Lines++;
                    for (int a=i;a>0;a--)
                    for (int b=0;b<GlassWidth;b++)
                         GlassMap[b][a]= GlassMap[b][a-1];

               }
     }
     for (int i=0;i<GlassHeight;i++)
     for (int j=0;j<GlassWidth;j++)
          if (OldGlassMap[j][i]!=GlassMap[j][i])
               if (GlassMap[j][i]==0) ClearBlock(j,i);
               else DrawBlock(j,i,Colors[GlassMap[j][i]-1]);
     switch(Lines){
     case 1: Score +=100;
          break;
     case 2: Score +=250;
          break;
     case 3: Score +=550;
          break;
     case 4: Score +=1200;
          break;
     }
     if (Lines>0&&PlaySnd) sndPlaySound("Line.wav",SND_ASYNC);
}

void __fastcall TTetrisForm::Timer1Timer(TObject *Sender)
{
     MoveDown();
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::Button2Click(TObject *Sender)
{
     if (Timer1->Enabled){
          Timer1->Enabled = false;
          Button3->Enabled = false;
     }
     else {
          Timer1->Enabled = true;
          Button3->Enabled = true;
     }     
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::Button3Click(TObject *Sender)
{
     Button1Click(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::X1Click(TObject *Sender)
{
     Close();
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::Button4Click(TObject *Sender)
{
     Close();
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::N2Click(TObject *Sender)
{
     if(OpenPictureDialog1->Execute()){
          ImageFileName=OpenPictureDialog1->FileName;
          LoadImage(ImageFileName);
     }
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::Button5Click(TObject *Sender)
{
     Application->HelpCommand(HELP_CONTENTS, 0); 
}
//---------------------------------------------------------------------------


void __fastcall TTetrisForm::N1Click(TObject *Sender)
{
     Application->HelpCommand(HELP_CONTENTS, 0);
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::N4Click(TObject *Sender)
{
     PlayMusic=!PlayMusic;
     N4->Checked=PlayMusic;
     if (PlayMusic) MediaPlayer1->Play();
          else MediaPlayer1->Stop();

}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::N5Click(TObject *Sender)
{
     PlaySnd=!PlaySnd;
     N5->Checked=PlaySnd;
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::MediaPlayer1Notify(TObject *Sender)
{
     if (MediaPlayer1->NotifyValue==nvSuccessful)
          MediaPlayer1->Play();
}
//---------------------------------------------------------------------------



void __fastcall TTetrisForm::LoadImage(AnsiString FileName)
{
     TImage *tmpImage=new TImage(this);
     tmpImage->AutoSize=true;
     tmpImage->Picture->LoadFromFile(FileName);
     if (tmpImage->Width>Image->Width&&tmpImage->Height>Image->Height)
          BGImage->Canvas->Draw(0,0,tmpImage->Picture->Graphic);
     else
          BGImage->Canvas->StretchDraw(
               Rect(0,0,Image->Width,Image->Height),
               tmpImage->Picture->Graphic);
     for (int i=0;i<GlassWidth;i++)
     for (int j=0;j<GlassHeight;j++)
          ClearBlock(i,j);
     delete tmpImage;
}


void __fastcall TTetrisForm::FormClose(TObject *Sender,
      TCloseAction &Action)
{
     IniFile->WriteBool("Option","Sound",PlaySnd);
     IniFile->WriteBool("Option","Music",PlayMusic);
     IniFile->WriteString("Option","BGImage",ImageFileName);
     for (int i=0;i<3;i++){
          IniFile->WriteInteger("HighScore","HighScore"+IntToStr(i),HighScore[i]);
          IniFile->WriteString("HighName","HighName"+IntToStr(i),HighName[i]);
     }
     delete IniFile;
}
//---------------------------------------------------------------------------


void TTetrisForm::ReadIni()
{
     IniFile=new TIniFile(
          ExtractFilePath(Application->ExeName)
          +"\\"+IniFileName);
     PlaySnd=IniFile->ReadBool("Option","Sound",true);
     PlayMusic=IniFile->ReadBool("Option","Music",true);
     ImageFileName=IniFile->ReadString("Option","BGImage","BG.bmp");
     for (int i=0;i<3;i++){
          HighScore[i]=IniFile->ReadInteger("HighScore","HighScore"+IntToStr(i),0);
          HighName[i]=IniFile->ReadString("HighName","HighName"+IntToStr(i),"匿名");
     }
     N4->Checked=PlaySnd;
     N5->Checked=PlayMusic;
}


void TTetrisForm::AddHighScore()
{
     for (int i=0;i<3;i++)
          if (Score>HighScore[i]){
               for (int j=2;j>i;j--){
                    HighScore[j]=HighScore[j-1];
                    HighName[j]=HighName[j-1];
               }
               HighScore[i]=Score;
               InputQuery("恭喜你!你进入了俄罗斯方块英雄榜",
               "请输入您的大名:",HighName[i]);
               T1Click(NULL);
               break;
          }
}

void __fastcall TTetrisForm::T1Click(TObject *Sender)
{
     ScoreForm->Label4->Caption=AnsiString(HighScore[0])+"分";
     ScoreForm->Label5->Caption=AnsiString(HighScore[1])+"分";
     ScoreForm->Label6->Caption=AnsiString(HighScore[2])+"分";
     ScoreForm->Label7->Caption=HighName[0];
     ScoreForm->Label8->Caption=HighName[1];
     ScoreForm->Label9->Caption=HighName[2];
     ScoreForm->Show();
}
//---------------------------------------------------------------------------


void __fastcall TTetrisForm::BCB1Click(TObject *Sender)
{
     AboutBox->Show();
}
//---------------------------------------------------------------------------

void __fastcall TTetrisForm::JoyMove(TMessage &msg)
{
     if (!isPlaying) return;
     int PosX,PosY;
     PosX=msg.LParamLo;
     PosY=msg.LParamHi;
     if (PosX<10000)
          MoveLeft();
     else if (PosX>55000)
          MoveRight();
     if (PosY>55000)
          MoveDown();

}
void __fastcall TTetrisForm::JoyPress(TMessage &msg)
{
     if (!isPlaying) return;
     if (msg.WParam & JOY_BUTTON1)
          Rotate();
}
void __fastcall TTetrisForm::J1Click(TObject *Sender)
{
     JOYINFO JoyInfo;
     J1->Checked=!J1->Checked;
     if (!J1->Checked)
          joyReleaseCapture(JOYSTICKID1);
     else {
          joyGetPos(JOYSTICKID1,&JoyInfo);
          joySetCapture(Handle,JOYSTICKID1,2*JoyCaps.wPeriodMin,false);
     }

}
//---------------------------------------------------------------------------


void __fastcall TTetrisForm::ConnectJoy()
{
     MMRESULT Result;
     JOYINFO JoyInfo;
     if (joyGetNumDevs()==0){
          J1->Enabled=false;
          return;
     }
     Result=joyGetPos(JOYSTICKID1,&JoyInfo);
     if (Result!=JOYERR_NOERROR){
          J1->Enabled=false;
          return;
     }

     if (J1->Checked){
          joyGetDevCaps(JOYSTICKID1,&JoyCaps,sizeof(JOYCAPS));
          joySetCapture(Handle,JOYSTICKID1,2*JoyCaps.wPeriodMin,false);
     }
}

void __fastcall TTetrisForm::FormDestroy(TObject *Sender)
{
     if (J1->Checked)
          joyReleaseCapture(JOYSTICKID1);
}
//---------------------------------------------------------------------------